﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Reporting.WebForms
Imports log4net

Partial Class Report_RP04
    Inherits System.Web.UI.Page

    Dim Report As Object
    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("Report_RP04")

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        'If Session("fundcenterS") = "" Then
        '    Session("fundcenterS") = "0011"
        '    Session("fundcenterE") = "0012"

        'End If

        'If Session("users") = "" Then
        '    Session("users") = "wassana"
        'End If
        'Session("year") = 2556
        'Session("version") = 2

        If Session("users") Is Nothing Then
            Response.Redirect("../LoginForm.aspx")
        End If


        Getdata()


    End Sub


    Function Getdata() As Decimal


        Dim adapter As New Data_ReportTableAdapters.R04TableAdapter
        Dim sql As String
        Dim dt As New Data_Report.R04DataTable

        sql = "  select  Assetgroup "
        sql += " , ISNULL(CONVERT(VARCHAR(2),Assetgroup) + '. ' + (SELECT TOP 1 AppConfigs.Description FROM AppConfigs WHERE AppConfigs.Name = 'ASSET_GROUP' AND AppConfigs.Value = activitytypes.AssetGroup),CONVERT(VARCHAR(2),Assetgroup) + '. ')  as AssetGroupName "
        sql += " ,activitytypecode ,  description , price"
        sql += "  from activitytypes"
        sql += "  where 1= 1"

        sql += " and NOT (Price = 0 OR Price IS NULL) and ActivityTypes.PlanYear = '" & Session("year") & "'"

        If Val(Session("Assetgroup")) <> 0 Then
            sql += "  and Assetgroup =  " & Session("Assetgroup")
        End If

        If Val(Session("ActivityTCFrom")) <> 0 And Val(Session("ActivityTCTo")) <> 0 Then
            sql += " and ActivityTypeCode between  '" & Session("ActivityTCFrom") & "' and '" & Session("ActivityTCTo") & "'"
        End If
        If Val(Session("ActivityTCFrom")) <> 0 And Val(Session("ActivityTCTo")) = 0 Then
            sql += " and  ActivityTypeCode = " & Session("ActivityTCFrom")
        End If
        If Val(Session("ActivityTCFrom")) = 0 And Val(Session("ActivityTCTo")) <> 0 Then
            sql += " and  ActivityTypeCode = " & Session("ActivityTCTo")
        End If
        'If Val(Session("ActivityTCFrom")) = 0 And Val(Session("ActivityTCTo")) = 0 Then
        '    sql += " and  ActivityTypeCode  between " & "'00000000'" & " and " & "'99999999'"
        'End If
        If Session("ActivityTCSelect") <> "" Then
            sql += " and ActivityTypeCode in ('" & Session("ActivityTCSelect") & "')"
        End If

        sql += " order by Assetgroup"

        'Response.Write(sql)
        'Exit Function
        adapter.SearchReport(dt, sql)

        dt.TableName = "R04DT"
        Dim das As New DataSet
        das.DataSetName = "R01DS"
        das.Tables.Add(dt)
        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local
        'ReportViewer1.LocalReport.ReportPath = System.Environment.CurrentDirectory & "Report\R01.rdlc"
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", das.Tables("R04DT")))
        ReportViewer1.DocumentMapCollapsed = True

        Dim UserName As String = Session("users")
        Dim years As String = Session("year")

        Dim users As String = Session("users")

        Dim p1, p2 As ReportParameter
        p1 = New ReportParameter("year", years)
        p2 = New ReportParameter("username", UserName)

        ReportViewer1.LocalReport.SetParameters(p1)
        ReportViewer1.LocalReport.SetParameters(p2)

        ReportViewer1.LocalReport.Refresh()

        'Response.Write(dt.Rows.Count & sql)
        Session("count") = dt.Rows.Count

        Return dt.Rows.Count
    End Function


    Public Function nPageNumber() As String
        Dim str As String
        str = Me.Report.Globals!PageNumber.ToString()
        Return str
    End Function

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            ScriptText = "<script> wclose();</script>"
        End If
    End Sub
End Class
